package chapter5.implement_initializingbean;

import org.springframework.beans.factory.InitializingBean;

/**
 * 
 * @author chauthang
 * @comment example implement InitializingBean call method afterPropertiesSet()
 *          when after create finish bean
 */
public class SimpleBeanWithInterface implements InitializingBean {
	private static final String DEFAULT_NAME = "Luke Skywalker";
	private String name = null;
	private int age = Integer.MIN_VALUE;

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Initializing bean");
		if (name == null) {
			System.out.println("Using default name");
			name = DEFAULT_NAME;
		}
		if (age == Integer.MIN_VALUE) {
			throw new IllegalArgumentException(
					"You must set the age property of any beans of type "
							+ SimpleBeanWithInterface.class);
		}
	}

	public String toString() {
		return "Name: " + name + "\nAge: " + age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
